/**
 * 作者: wgx
 * 日期: 2025-02-17 10:43:02
 * 备注: 老虎机抽奖获得招募券记录表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.DrawManager;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

public class TigerDrawRecord extends TigerDrawRecordModel {
    public TigerDrawRecord() {
        super();
    }

    public TigerDrawRecord(int serverId, long userId, String userName, int itemId, long itemNum) {
        super();
        putServerId(serverId);
        putUserId(userId);
        putUserName(userName);
        putItemId(itemId);
        putItemNum(itemNum);
        putAddTime(TimeUtils.getCurTime());
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public JsonObject insert() {
        // 如果超过上限则会删除最早的那条
        long count = Future.await(RedisTools.getHashLen(getListKey()));
        if (count >= AllParam.TIGER_DRAW_RECORD_MAX_NUM) {
            String orderListKey = EntityManager.getOrderListKey(getListKey());
            String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
            long id = Long.parseLong(delRecord);// dbid
            TigerDrawRecord tigerDrawRecord = DrawManager.getTigerDrawRecord(getServerId(), id);
            if (tigerDrawRecord != null) {
                tigerDrawRecord.delete();
            }
        }
        return super.insert();
    }
}
